package com.admission.web;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Message;
import com.admission.service.MessageManager;
import com.admission.util.PageUtils;

@Namespace("/base")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "redirect") })
public class MessageAction extends CrudActionSupport<Message> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id;
	private MessageManager messageManager;
	private Message entity;
	private Page<Message> page = new Page<Message>(PageUtils.MAIN_ENROLL);
	private List<Message> messageList;
	private String status="";

	public Message getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		if (status.equals("1")) {
			PropertyFilter p = new PropertyFilter("EQS_tag", Message.QUALITY);
			filters.add(p);
		}
		PropertyFilter p1 = new PropertyFilter("EQS_replyTag", Message.REPLYTRUE);
		filters.add(p1);
		page.setOrder(Page.DESC);
		page.setOrderBy("editTime");
		page = messageManager.pageByFilter(page, filters);
		return SUCCESS;
	}

	public String asking() throws Exception {
		return "asking";
	}

	/**
	 * 判断留言板状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkOnlineConsult() throws Exception {
		if (messageManager.checkConsult().equals("1"))
			return "asking";
		return "sysfail";
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		try {
			messageManager.save(entity);
		} catch (Exception e) {
			this.addActionMessage("操作失败，请重新操作");
		}
		return list();
	}

	@Override
	public String delete() throws Exception {
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new Message();
		} else {
			entity = messageManager.getById(id);
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public MessageManager getMessageManager() {
		return messageManager;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}

	public Message getEntity() {
		return entity;
	}

	public void setEntity(Message entity) {
		this.entity = entity;
	}

	public Page<Message> getPage() {
		return page;
	}

	public void setPage(Page<Message> page) {
		this.page = page;
	}

	public List<Message> getMessageList() {
		return messageList;
	}

	public void setMessageList(List<Message> messageList) {
		this.messageList = messageList;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

}
